package domParseXML;
import java.io.FileOutputStream;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
 *  使用DOM来解析XML文档
 *  	
 */


public class Test {

	public static void main(String[] args) {
		showItemByNum(3);
		//savePhone();
		//modify();
		//delete();
	}

	public static void delete(){
		try {
			// 1、得到DOM解析器的工厂实例
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			// 2、从DOM工厂获得DOM解析器
			DocumentBuilder db = dbf.newDocumentBuilder();
			// 3、解析XML文档，得到一个Document，即DOM树
			Document doc = db.parse("src/收藏信息.xml");
			//找到删除的节点
			NodeList list=doc.getElementsByTagName("Brand");
			for(int i=0;i<list.getLength();i++){
				Element brandElement=(Element)list.item(i);
				String brandName=brandElement.getAttribute("name");
				if(brandName.equals("联想")){
					brandElement.getParentNode().removeChild(brandElement);
				}
			}
			
			
			
			

			//保持XML文件
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource domSource = new DOMSource(doc);
			//设置编码类型
			transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
			StreamResult result = new StreamResult(new FileOutputStream("src/收藏信息.xml"));
			//把DOM树转换为XML文件
			transformer.transform(domSource, result);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void modify(){
		try {
			// 1、得到DOM解析器的工厂实例
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			// 2、从DOM工厂获得DOM解析器
			DocumentBuilder db = dbf.newDocumentBuilder();
			// 3、解析XML文档，得到一个Document，即DOM树
			Document doc = db.parse("src/收藏信息.xml");
			//找到修改的节点
			NodeList list=doc.getElementsByTagName("Brand");
			for(int i=0;i<list.getLength();i++){
				Element brandElement=(Element)list.item(i);
				String brandName=brandElement.getAttribute("name");
			    if(brandName.equals("苹果")){
			    	//修改属性
			    	brandElement.setAttribute("name", "APPLE");
			    }
			}
			
			
			
			

			//保持XML文件
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource domSource = new DOMSource(doc);
			//设置编码类型
			transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
			StreamResult result = new StreamResult(new FileOutputStream("src/收藏信息.xml"));
			//把DOM树转换为XML文件
			transformer.transform(domSource, result);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public static void savePhone() {
		try {
			// 1、得到DOM解析器的工厂实例
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			// 2、从DOM工厂获得DOM解析器
			DocumentBuilder db = dbf.newDocumentBuilder();
			// 3、解析XML文档，得到一个Document，即DOM树
			Document doc = db.parse("src/收藏信息.xml");
			//创建Brand节点
			Element brandElement= doc.createElement("Brand");
			brandElement.setAttribute("name", "联想");
			//添加Type节点
			Element typeElement=doc.createElement("Type");
			typeElement.setAttribute("name", "U8650");
			//添加父子关系
			brandElement.appendChild(typeElement);
			Element phoneElement=(Element)doc.getElementsByTagName("PhoneInfo").item(0);
			phoneElement.appendChild(brandElement);
			
			
			
			

			//保持XML文件
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource domSource = new DOMSource(doc);
			//设置编码类型
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			StreamResult result = new StreamResult(new FileOutputStream("src/收藏信息.xml"));
			//把DOM树转换为XML文件
			transformer.transform(domSource, result);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static List<Item> showItem() {
		try {
			// 1、得到DOM解析器的工厂实例
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			// 2、从DOM工厂获得DOM解析器
			DocumentBuilder db = dbf.newDocumentBuilder();
			// 3、解析XML文档，得到一个Document，即DOM树
			Document doc = db.parse("src/网易手机各地行情.xml");
			// 4、读取新闻
			NodeList list = doc.getElementsByTagName("item");

			ArrayList<Item> itemList = new ArrayList<Item>();
			for (int i = 0; i < list.getLength(); i++) {
				Item item = new Item();//new 一个Item对象

				Element itemElement = (Element) list.item(i);//第i个item节点
				String title = itemElement.getElementsByTagName("title")//itemElement.getElementsByTagName("title")得到一个title元素,item(0)得到第一个title节点.
						.item(0).getFirstChild().getNodeValue();//得到title元素的第一个节点的第一个子节点(title节点的文本节点)的节点值
				String description = itemElement.getElementsByTagName(
						"description").item(0).getFirstChild().getNodeValue();
				String link = itemElement.getElementsByTagName("link").item(0)
						.getFirstChild().getNodeValue();
				String pubDate = itemElement.getElementsByTagName("pubDate")
						.item(0).getFirstChild().getNodeValue();
                //讲解析XML文档的到的值赋予Item对象
				item.setDescription(description);
				item.setLink(link);
				item.setPubDate(pubDate);
				item.setTitle(title);
                 //讲item对象添加到ItemList集合中去
				itemList.add(item);

			}
			return itemList;

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
			return null;
		}
	}

	public static void showItemByNum(int num) {
		List<Item> listItem = showItem();
		Item item = listItem.get(num);
		System.out.println(num + "\t" + item.getPubDate() + "\t"
				+ item.getTitle());
	}

}
